{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/drive/1P5RHD7UBfPTMhgQTJ0i4DK4ScsYZuTOU?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "\n",
        "### [Groq](https://groq.com/) - Groq is Fast AI Inference for open source LLM API calls\n"
      ],
      "metadata": {
        "id": "HxxOWz9dpsYj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Install required libraries"
      ],
      "metadata": {
        "id": "Y3axTI0sp5Hg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q -U langchain-groq==0.2.4"
      ],
      "metadata": {
        "id": "ShxTNxM5gqtr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Import Groq related libraries"
      ],
      "metadata": {
        "id": "9jJ1vqs-p_Zx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_groq import ChatGroq"
      ],
      "metadata": {
        "id": "RL-3LsYogoH5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import getpass\n",
        "import os"
      ],
      "metadata": {
        "id": "GT55z5AkhyOW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Provide a Groq API key. You can create one to access free open-source models at the following link.\n",
        "\n",
        "[Groq API Creation Link](https://console.groq.com/keys)\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "F6UeDlrgqI2A"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"GROQ_API_KEY\"] = getpass.getpass()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yobvrD3glfd4",
        "outputId": "4b5a2cf1-2172-499c-9db0-0e32d0bde00f"
      },
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "··········\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "llm = ChatGroq(\n",
        "    model=\"llama3-8b-8192\",\n",
        "    temperature=0.5\n",
        ")\n",
        "\n",
        "messages = [\n",
        "    (\n",
        "        \"system\",\n",
        "        \"You are a helpful assistant that explains problems step-by-step.\",\n",
        "    ),\n",
        "    (\"human\", \"Solve this problem step by step: {problem}\"),\n",
        "]"
      ],
      "metadata": {
        "id": "B5I5PCsKNP2N"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "ai_msg = llm.invoke(\"Provide me python code of sudoku\")\n",
        "print(ai_msg.content)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LgHiTuGCogpC",
        "outputId": "dc7d2f34-80b7-432c-c78e-4816bf5cce28"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Here is a simple implementation of Sudoku in Python:\n",
            "\n",
            "```Python\n",
            "def is_valid(board, row, col, num):\n",
            "    # Check the row\n",
            "    for x in range(9):\n",
            "        if board[row][x] == num:\n",
            "            return False\n",
            "\n",
            "    # Check the column\n",
            "    for x in range(9):\n",
            "        if board[x][col] == num:\n",
            "            return False\n",
            "\n",
            "    # Check the box\n",
            "    start_row = row - row % 3\n",
            "    start_col = col - col % 3\n",
            "    for i in range(3):\n",
            "        for j in range(3):\n",
            "            if board[i + start_row][j + start_col] == num:\n",
            "                return False\n",
            "    return True\n",
            "\n",
            "\n",
            "def solve_sudoku(board):\n",
            "    for i in range(9):\n",
            "        for j in range(9):\n",
            "            if board[i][j] == 0:\n",
            "                for num in range(1, 10):\n",
            "                    if is_valid(board, i, j, num):\n",
            "                        board[i][j] = num\n",
            "                        if solve_sudoku(board):\n",
            "                            return True\n",
            "                        board[i][j] = 0\n",
            "                return False\n",
            "    return True\n",
            "\n",
            "\n",
            "def print_board(board):\n",
            "    for i in range(9):\n",
            "        if i % 3 == 0 and i != 0:\n",
            "            print(\"- - - - - - - - - - - -\")\n",
            "\n",
            "        for j in range(9):\n",
            "            if j % 3 == 0 and j != 0:\n",
            "                print(\" | \", end=\"\")\n",
            "\n",
            "            if j == 8:\n",
            "                print(board[i][j])\n",
            "            else:\n",
            "                print(str(board[i][j]) + \" \", end=\"\")\n",
            "\n",
            "\n",
            "# Example usage:\n",
            "board = [\n",
            "    [5, 1, 7, 6, 0, 0, 0, 3, 4],\n",
            "    [2, 8, 9, 0, 0, 4, 0, 0, 0],\n",
            "    [3, 4, 6, 2, 0, 5, 0, 9, 0],\n",
            "    [6, 0, 2, 0, 0, 0, 0, 1, 0],\n",
            "    [0, 3, 8, 0, 0, 6, 0, 4, 7],\n",
            "    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
            "    [0, 9, 0, 0, 0, 0, 0, 7, 8],\n",
            "    [7, 0, 3, 4, 0, 0, 5, 6, 0],\n",
            "    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
            "]\n",
            "\n",
            "if solve_sudoku(board):\n",
            "    print_board(board)\n",
            "else:\n",
            "    print(\"No solution exists\")\n",
            "```\n",
            "\n",
            "This program uses a backtracking algorithm to solve the Sudoku puzzle. It starts by filling in the first empty cell with a number from 1 to 9. If the number is valid (i.e., it doesn't already appear in the same row, column, or 3x3 box), it recursively fills in the rest of the board. If it reaches a point where it can't fill in a number, it backtracks and tries a different number in the previous cell. If it can't fill in any number, it backtracks until it reaches a cell that can be filled in, and then tries a different number.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "3rDg4_fZtmnp"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}